//***************************************************************
//  File:    GameObject.cpp
//  Date:    12/17/2012
//  Author:  liangxc (liangxc2010@gmail.com)
//-------------------------------------------------------------
//
//-------------------------------------------------------------
//  This file is part of Hare2D Game Engine.
//  Copyright (C) All Rights Reserved
//***************************************************************
//
//***************************************************************
#include "PCH.h"
#include "GameObject.h"

#include "SpriteComponent.h"

namespace hare
{
	HARE_IMPLEMENT_DYNAMIC_CLASS(GameObject, Object, 0)
	{
		HARE_META(enable, bool)
		HARE_META(position, PointF)
		HARE_META(rotation, PointF)
		HARE_META(scale, PointF)
		HARE_OBJ_LIST_F(componentList, Component, propAllowSubs)
	}

	GameObject::GameObject()
		: enable(true)
		, position(0, 0)
		, rotation(0, 0)
		, scale(0, 0)
		, parent(NULL)
		, child(NULL)
		, prev(NULL)
		, next(NULL)
	{
		
	}

	GameObject::~GameObject()
	{

	}

	void GameObject::update()
	{
		Component::List::iterator it = componentList.begin();
		for (; it != componentList.end(); ++it)
		{
			if ((*it)->getEnable())
				(*it)->update();
		}
	}

	void GameObject::render()
	{
		Component::List::iterator it = componentList.begin();
		for (; it != componentList.end(); ++it)
		{
			if ((*it)->getEnable())
				(*it)->render(position, rotation, scale);
		}
	}

	bool GameObject::getEnable()
	{
		return enable;
	}

	void GameObject::setEnable(bool flag)
	{
		enable = flag;
	}

	const PointF& GameObject::getPosition()
	{
		return position;
	}

	void GameObject::setPosition(float x, float y)
	{
		position.x = x;
		position.y = y;
	}

	void GameObject::move(float x, float y)
	{
		position.x += x;
		position.y += y;
	}

	const PointF& GameObject::getRotation()
	{
		return rotation;
	}

	void GameObject::setRotation(float x, float y)
	{
		rotation.x = x;
		rotation.y = y;
	}

	const PointF& GameObject::getScale()
	{
		return scale;
	}

	void GameObject::setScale(float x, float y)
	{
		scale.x = x;
		scale.y = y;
	}

	void GameObject::AddComponent()
	{
		Component::Ptr newComponent = (Component*)SpriteComponent::CLASS_INFO.createObject();
		if (newComponent)
			newComponent->addRef();
		
		componentList.push_back(newComponent);
	}
}